package com.cloudpolice.equipment.controller;

import com.cloudpolice.equipment.model.EquipmentAdvertisingModel;
import com.cloudpolice.equipment.model.InPutParam.EquipmentAdvertisingSelectInParam;
import com.cloudpolice.equipment.service.EquipmentAdvertisingService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lh.model.ResultVO;
import lh.model.ResultVOPage;
import lh.units.ResultStruct;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;
import java.util.List;

/**
 * @author ：梁昊，后端工程师：flyman5，前端工程师：孙耀
 * @create 2020-04-02 12:44
 * @function
 * @editLog
 */
@RestController
@RequestMapping("/equipmentAdvertisingController")
@Api(value = "设备粒子层", description = "门禁机广告")
public class EquipmentAdvertisingController {
    @Autowired
    EquipmentAdvertisingService equipmentAdvertisingService;
    /**
     * 增加门禁机广告，方法ID：IN2020040213552868528C3E25B1C17
     *
     * @param id 图片ID（主键）
     * @param communityId 社区ID
     * @param periodId 社区区域ID
     * @param sortNo 出现顺序
     * @param beginDate 发布开始日期
     * @param endDate 发布结束日期
     * @return 广告列表
     */
    @ApiOperation(value = "增加门禁机广告,方法ID：IN2020040213552868528C3E25B1C17", notes = "广告列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "图片ID（主键）", dataType = "String", required = true)
            , @ApiImplicitParam(name = "communityId", value = "社区ID", dataType = "String", required = true)
            , @ApiImplicitParam(name = "periodId", value = "社区区域ID", dataType = "String", required = true)
            , @ApiImplicitParam(name = "sortNo", value = "出现顺序", dataType = "String", required = true)
            , @ApiImplicitParam(name = "beginDate", value = "发布开始日期", dataType = "Date", required = true)
            , @ApiImplicitParam(name = "endDate", value = "发布结束日期", dataType = "Date", required = true)
    })
    @PostMapping("/insertEquipmentAdvertising")
    public ResultVO insertEquipmentAdvertising(@RequestParam(value = "id") String id
            , @RequestParam(value = "communityId") String communityId
            , @RequestParam(value = "periodId") String periodId
            , @RequestParam(value = "sortNo") int sortNo
            , @RequestParam(value = "beginDate") @DateTimeFormat(pattern = "yyyy-MM-dd") Date beginDate
            , @RequestParam(value = "endDate") @DateTimeFormat(pattern = "yyyy-MM-dd") Date endDate) {
        communityId = communityId == null ? communityId : communityId.trim();
        periodId = periodId == null ? periodId : periodId.trim();

        EquipmentAdvertisingModel equipmentAdvertisingModel = new EquipmentAdvertisingModel();
        equipmentAdvertisingModel.setId(id);
        equipmentAdvertisingModel.setCommunityId(communityId);
        equipmentAdvertisingModel.setPeriodId(periodId);
        equipmentAdvertisingModel.setSortNo(sortNo);
        equipmentAdvertisingModel.setBeginDate(beginDate);
        equipmentAdvertisingModel.setEndDate(endDate);
        int resultCount = equipmentAdvertisingService.insertEquipmentAdvertising(equipmentAdvertisingModel);
        if (resultCount > 0)
            return ResultStruct.success(resultCount);
        else if (resultCount == -1)
            return ResultStruct.error("本广告图片已经在本社区存在，不允许重复增加！", ResultVO.class, int.class);
        else
            return ResultStruct.error("增加失败", ResultVO.class, int.class);
    }

    /**
     * 修改指定门禁机广告，方法ID：UP2020040213273633191B3C52E2BA1
     *
     * @param id        图片ID（主键）
     * @param sortNo    出现顺序
     * @param beginDate 发布开始日期
     * @param endDate   发布结束日期
     * @return 广告列表
     */
    @ApiOperation(value = "修改指定门禁机广告,方法ID：UP2020040213273633191B3C52E2BA1", notes = "广告列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "图片ID（主键）", dataType = "String", required = true)
            , @ApiImplicitParam(name = "communityId", value = "社区ID", dataType = "String", required = true)
            , @ApiImplicitParam(name = "periodId", value = "社区区域ID", dataType = "String", required = true)
            , @ApiImplicitParam(name = "sortNo", value = "出现顺序", dataType = "String", required = true)
            , @ApiImplicitParam(name = "beginDate", value = "发布开始日期", dataType = "Date", required = true)
            , @ApiImplicitParam(name = "endDate", value = "发布结束日期", dataType = "Date", required = true)
    })
    @PostMapping("/updateEquipmentAdvertisingById")
    public ResultVO updateEquipmentAdvertisingById(@RequestParam(value = "id") String id
            , @RequestParam(value = "communityId") String communityId
            , @RequestParam(value = "periodId") String periodId
            , @RequestParam(value = "sortNo") int sortNo
            , @RequestParam(value = "beginDate") @DateTimeFormat(pattern = "yyyy-MM-dd") Date beginDate
            , @RequestParam(value = "endDate") @DateTimeFormat(pattern = "yyyy-MM-dd") Date endDate
    ) {
        id = id == null ? id : id.trim();
        communityId = communityId == null ? communityId : communityId.trim();
        periodId = periodId == null ? periodId : periodId.trim();

        EquipmentAdvertisingModel equipmentAdvertisingModel = new EquipmentAdvertisingModel();
        equipmentAdvertisingModel.setId(id);
        equipmentAdvertisingModel.setCommunityId(communityId);
        equipmentAdvertisingModel.setPeriodId(periodId);
        equipmentAdvertisingModel.setSortNo(sortNo);
        equipmentAdvertisingModel.setBeginDate(beginDate);
        equipmentAdvertisingModel.setEndDate(endDate);
        int updateCount = equipmentAdvertisingService.updateEquipmentAdvertisingById(equipmentAdvertisingModel);
        if (updateCount > 0)
            return ResultStruct.success(updateCount);
        else
            return ResultStruct.error("修改失败", ResultVO.class, int.class);
    }

    /**
     * 自动删除门禁机广告，方法ID：DE20200402125853418D4A65B5394A1
     *
     * @return 影响条数
     */
    @ApiOperation(value = "自动删除门禁机广告,方法ID：DE20200402125853418D4A65B5394A1", notes = "影响条数")
    @PostMapping("/deleteEquipmentAdvertisingAuto")
    public int deleteEquipmentAdvertisingAuto() {
        EquipmentAdvertisingModel equipmentAdvertisingModel = new EquipmentAdvertisingModel();
        equipmentAdvertisingModel.setEndDate(new Date());
        int updateCount = equipmentAdvertisingService.deleteEquipmentAdvertisingById(equipmentAdvertisingModel);

        return updateCount;
    }

    /**
     * 删除门禁机广告，方法ID：DE20200402125853418D4A65B5394A1
     *
     * @param id          图片ID（主键）
     * @param communityId 社区ID
     * @param periodId    社区区域ID
     * @return 影响条数
     */
    @ApiOperation(value = "删除门禁机广告,方法ID：DE20200402125853418D4A65B5394A1", notes = "影响条数")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "图片ID（主键）", dataType = "String", required = true)
            , @ApiImplicitParam(name = "communityId", value = "社区ID", dataType = "String", required = true)
            , @ApiImplicitParam(name = "periodId", value = "社区区域ID", dataType = "String", required = true)
    })
    @PostMapping("/deleteEquipmentAdvertisingById")
    public int deleteEquipmentAdvertisingById(@RequestParam(value = "id") String id
            , @RequestParam(value = "communityId") String communityId
            , @RequestParam(value = "periodId") String periodId
    ) {
        id = id == null ? id : id.trim();
        communityId = communityId == null ? communityId : communityId.trim();
        periodId = periodId == null ? periodId : periodId.trim();
        EquipmentAdvertisingModel equipmentAdvertisingModel = new EquipmentAdvertisingModel();
        equipmentAdvertisingModel.setId(id);
        equipmentAdvertisingModel.setCommunityId(communityId);
        equipmentAdvertisingModel.setPeriodId(periodId);
        if (id == null) {
            equipmentAdvertisingModel.setEndDate(new Date());
        }
        int updateCount = equipmentAdvertisingService.deleteEquipmentAdvertisingById(equipmentAdvertisingModel);

        return updateCount;
    }


    /**
     * 得到门禁机广告，方法ID：SE20200402124026176C7D091631575
     *
     * @param communityId 表vou_EquitmentAdvertising,字段名communityId:社区ID
     * @param periodId    表vou_EquitmentAdvertising,字段名periodId:社区区域ID
     * @return 广告列表
     */
    @ApiOperation(value = "得到门禁机广告,方法ID：SE20200402124026176C7D091631575", notes = "广告列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "communityId", value = "社区ID", dataType = "String", required = true),
            @ApiImplicitParam(name = "periodId", value = "社区区域ID", dataType = "String", required = true),
    })
    @PostMapping("/selectEquipmentAdvertising")
    public ResultVO selectEquipmentAdvertising(@RequestParam(value = "communityId") String communityId
            , @RequestParam(value = "periodId") String periodId
    ) {
        communityId = communityId == null ? communityId : communityId.trim();
        periodId = periodId == null ? periodId : periodId.trim();
        final Date date = new Date();
        EquipmentAdvertisingSelectInParam equipmentAdvertisingSelectInParam = new EquipmentAdvertisingSelectInParam();
        equipmentAdvertisingSelectInParam.setCommunityId(communityId);
        equipmentAdvertisingSelectInParam.setPeriodId(periodId);
        equipmentAdvertisingSelectInParam.setBeginDate(date);
        equipmentAdvertisingSelectInParam.setEndDate(date);
        final List<EquipmentAdvertisingModel> equipmentAdvertisingModels = equipmentAdvertisingService.selectEquipmentAdvertising(equipmentAdvertisingSelectInParam);
        return ResultStruct.success(equipmentAdvertisingModels, equipmentAdvertisingModels.size());
    }
}
